import { areaData } from '../../../../data/area';
import { addressData } from '../../../../data/address';
const app = getApp();
Page({
  data:{
    rname:'',
    phone:'',
    area:'',
    address:'',
    areaData,
    isAreaShowed:false,
    tags:[{id:0,name:"公司"},{id:1,name:"家"},{id:2,name:"父母老家"},{id:3,name:"老婆单位"},{id:4,name:"儿子学校宿舍"}],
    currentTagIndex:-1,
    tagInputShowed:false,
    isDefault:false,
    saveBtnDisabled:true,
    addressIndex:-1
  },
  onLoad(option) {
    let item;
    if(option.index>-1){
      this.setData({addressIndex:option.index});
    }else{
      this.setData({addressIndex:-1});
    }
    if(option.index>-1){
      item = addressData[option.index];
      this.setData({currentTagIndex:item.tag});
    }else if(app.gData.currentAddress&& option.index!=-1){
      item = app.gData.currentAddress;
    }
    if(item) {
      this.setData({
        rname: item.rname,
        phone: item.phone,
        area: item.area,
        address: item.address
      });
      this.setData({saveBtnDisabled: this.checkSaveBtnDisabled(this)});
    }
    //
    this.setData({isDefault:option.index==0});
  },
  onShow() {
    this.setData({tagInputShowed:false,isAreaShowed:false});
  },
  checkSaveBtnDisabled(that) {
    return that.data.rname=='' || that.data.phone=='' || that.data.area=='' || that.data.address=='';
  },
  onDefaultChange(e) {
    this.setData({ isDefault: e.detail });
  },
  showTagInput(e) {
    this.setData({tagInputShowed:true});
  },
  deleteTag(e) {
    console.log('deleteTag:',e);
    let _tags = this.data.tags;
    if (_tags.length < 1 || e.detail.index == -1) return;
    _tags = _tags.filter((item, index) => index != e.detail.index);
    this.setData({ tags: _tags, currentTagIndex: -1 });
  },
  beforeCloseTagInput(e) {
    //console.log('beforeCloseTagInput:',e.detail);
    if(e.detail.action=='cancel'){
      this.setData({tagInputShowed:false});
    }else {
      if(!e.detail.value){
        wx.showToast({
          title: '请填写标签信息',
        });
        return;
      }
      let _tags=this.data.tags;
      _tags.push({id:_tags.length,name:e.detail.value});
      this.setData({tags:_tags});
      this.setData({tagInputShowed:false});
    }
  },
  getWXAddress(e) {
    wx.chooseAddress({
      success: async (options) => {
        const { provinceName, cityName, countyName, detailInfo, userName, telNumber } = options;
        this.setData({
          rname: userName,
          phone: telNumber,
          area: provinceName+"/"+cityName+"/"+countyName,
          address: detailInfo
        });
        this.setData({saveBtnDisabled: this.checkSaveBtnDisabled(this)});
      }
    });
  },
  showArea(e) {
    this.setData({isAreaShowed: true,});
  },
  onAreaClose(e) {
    this.setData({isAreaShowed: false,});
  },
  onAreaFinish(e) {
    const { selectedOptions, value } = e.detail;
    const fieldValue = selectedOptions
        .map((option) => option.text || option.name)
        .join('/');
    console.log('fieldValue:',fieldValue);
    //console.log('cascaderValue:',value);
    this.setData({
      area:fieldValue,
    });
    this.onPopClose();
    this.setData({saveBtnDisabled: this.checkSaveBtnDisabled(this)});
  },
  onPopClose(e) {
    this.setData({isAreaShowed: false,});
  },
  save(e) {
    if(this.data.saveBtnDisabled){
      wx.showToast({title: '请先填写完资料'});
      return;
    }
    let item={
      rname:this.data.rname,
      phone:this.data.phone,
      area:this.data.area,
      address:this.data.address,
      tag:this.data.currentTagIndex
    };
    if(this.data.addressIndex>=0){
      if(this.data.isDefault){
        addressData.splice(this.data.addressIndex,1);
        addressData.unshift(item);
      }else{
        addressData[this.data.addressIndex] = item;
      }
    }else{
      if(this.data.isDefault)
        addressData.unshift(item);
      else
        addressData.push(item);
    }
    wx.navigateTo({url: '/pages/usercenter/address/list/index'});
  },
  onInput(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail;
    if(name=='收货人') this.setData({rname: value});
    if(name=='手机') this.setData({phone: value});
    if(name=='地区') this.setData({area: value});
    if(name=='详细地址') this.setData({address: value});
    this.setData({saveBtnDisabled: this.checkSaveBtnDisabled(this)});
  },
  selectTag(e) {
    this.setData({currentTagIndex:e.detail.index});
  }
})
